package com.tosit.bmx.controller.admin;

import com.tosit.bmx.common.json.JSONModel;
import com.tosit.bmx.common.json.JSONUtil;
import com.tosit.bmx.domian.Area;
import com.tosit.bmx.domian.AreaUser;
import com.tosit.bmx.service.admin.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 区域控制器
 *
 * @author tsl
 * @create 2018-04-22 16:07
 **/
@Controller
@RequestMapping()
public class AreaController {
    @Autowired
    AreaService areaService;

    /**
     * 添加一个区域
     *
     * @param area 区域对象
     * @return
     * @author tsl
     * @date 2018/4/22 16:17
     */
    @RequestMapping(value = "/area", method = RequestMethod.POST)
    public void addArea(Area area, HttpServletResponse response) {
        Integer result = areaService.addArea(area);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 删除一个区域
     *  js调用具体使用可以参考web下 Test.jsp
     * @param id       地区id
     * @param response
     * @author tsl
     * @date 2018/4/22 16:17
     */
    @RequestMapping(value = "/area", method = RequestMethod.DELETE)
    public void removeArea(Integer id, HttpServletResponse response) {
        Integer result = areaService.removeArea(id);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 修改一个区域
     *
     * @param area
     * @param response
     * @author tsl
     * @date 2018/4/22 16:17
     */
    @RequestMapping(value = "/area", method = RequestMethod.PUT)
    public void modifyArea(Area area, HttpServletResponse response) {
        System.out.println(area);
        Integer result = areaService.modifyArea(area);

        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 获取所有区域
     *
     * @param response
     * @author tsl
     * @date 2018/4/22 16:17
     */
    @RequestMapping(value = "/area", method = RequestMethod.GET)
    public void gainArea(HttpServletResponse response) {
        List<Area> areaList = areaService.gainArea();
        if (areaList != null) {
            JSONModel.put("status", "success");
            JSONModel.put("data", areaList);
            JSONUtil.printByJSON(response, JSONModel.put());
        } else {
            JSONModel.put("status", "fail");
            JSONUtil.printByJSON(response, JSONModel.put());
        }
    }


    /**
     * 删除多个区域
     * js调用具体使用可以参考web下 Test.jsp
     * @param ids      删除的所有地区id
     * @param response
     * @author tsl
     * @date 2018/4/22 16:17
     */
    @RequestMapping(value = "/areas", method = RequestMethod.DELETE)
    public void removeArea(@RequestBody List<Integer> ids, HttpServletResponse response) {
        Integer result = areaService.removeArea(ids);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }
    /**
     * 关联一个或多个用户
     * @author tsl  
     * @date 2018/4/23 9:29  
     * @param userIds 一个或多个用户（uid,aid）
     * @return 
     */  
    @RequestMapping(value = "/area/relateUser", method = RequestMethod.POST)
    public void relateUser(@RequestBody List<AreaUser> userIds, HttpServletResponse response) {
        Integer result = areaService.relateUser(userIds);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


    /**
     * 删除关联的一个或多个用户
     * @author tsl
     * @date 2018/4/23 9:29
     * @param auserIds 一个或多个用户id
     * @return
     */
    @RequestMapping(value = "/area/deleteUser", method = RequestMethod.DELETE)
    public void deleteUser(@RequestBody List<Integer> auserIds, HttpServletResponse response) {
        Integer result = areaService.deleteUser(auserIds);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


    /**
     * 修改一个或多个用户的关联区域
     * @author tsl
     * @date 2018/4/23 9:29
     * @param userIds 一个或多个用户（uid,aid）
     * @return
     */
    @RequestMapping(value = "/area/updateUser", method = RequestMethod.POST)
    public void updateUser(@RequestBody List<AreaUser> userIds, HttpServletResponse response) {
        Integer result = areaService.updateUser(userIds);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }



}
